// /*
//  * @Author: WanYiMing
//  * @Date:2024-01-22-15:44
//  * @Description:
//  */

using System;
using System.Collections.Generic;
using QFramework;

namespace Code.Utility
{
    public class RandomUtils:IUtility
    {
        
        public bool GetRandomBoolForRate(double probability)
        {
            Random random = new Random();
            bool result = random.NextDouble() < probability;
            return result;
        }
        
        
        public int GetRandomIntForMaxInt(int size)
        {
            Random random = new Random();
            int randomIndex = random.Next(size); // 生成随机索引
            return randomIndex;
        }
        
        
        
        public string GetRandomDictionaryKey(IEnumerable<string> keys,int count)
        {
            // 生成一个0到字典大小之间的随机数
            int randomIndex = new Random().Next(count);

            // 遍历字典，找到对应随机数的元素
            int currentIndex = 0;

            foreach (var key in keys)
            {
                if (currentIndex == randomIndex)
                {
                    return key;
                }
                currentIndex++;
            }

            throw new InvalidOperationException("Dictionary is empty");
        }
    }
}